package com.ruoyi.radar.cal;

import com.ruoyi.radar.cal.PlyHeader;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class PlyReader {
    private PlyHeader header;

    public PlyReader(PlyHeader header) {
        this.header = header;
    }

    public List<Vertex> readVertices(String filePath) throws IOException {
        List<Vertex> vertices = new ArrayList<>();
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            boolean inBody = false;
            int vertexCount = header.getElementCount("vertex");
            Map<String, String> vertexProperties = header.getElementProperties("vertex");

            while ((line = reader.readLine()) != null) {
                if (line.equals("end_header")) {
                    inBody = true;
                    continue;
                }
                if (inBody && vertexCount > 0) {
                    String[] tokens = line.split("\\s+");
                    float x = Float.parseFloat(tokens[0]);
                    float y = Float.parseFloat(tokens[1]);
                    float z = Float.parseFloat(tokens[2]);
                    vertices.add(new Vertex(x, y, z));
                    vertexCount--;
                }
            }
        }
        return vertices;
    }

    public static class Vertex {
        public float x, y, z;

        public Vertex(float x, float y, float z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
    }
}  